<?php
  class model {

  	protected $db;

     function __construct(&$db){
     $this->db=&$db;
    }

    function get_info($id="",$arg="",$sql="", $table="ns_tree",$arg_sql="id"){
      if ($sql !=""){
      $tmp=$this->db->result_array($this->db->query($sql));
         }else{
     	 		$tmp=$this->db->result_array($this->db->query("SELECT $arg From $table WHERE $arg_sql='$id' LIMIT 1;"));
         };

      if($arg !=""){
      		return $tmp [0] [$arg];

      	}else{

        return $tmp;
      	}



     }

	function save($id,$name,$show){
     $this->db->query("UPDATE ns_tree SET name='$name', show_me='$show' WHERE id='$id'");
	}

	function save_d($id,$edit,$name,$father_id,$show){
     $this->db->query("UPDATE ns_doc SET name='$name', text='$edit',father_id='$father_id', show_me='$show' WHERE id='$id'");
	}

	function save_doc($id,$edit,$name,$show){     $this->db->query(" INSERT INTO ns_doc VALUES (0,'$edit',CURDATE(),'$id','$show','$name');");
	}



      function add_in($id,$name,$show_me=true,$adress="",$side="left"){       $level=$this->get_info($id,"level");
       $level++;       $this->db->query(" BEGIN;
        SELECT @treeRight := rgt FROM ns_tree WHERE id = '$id';
       UPDATE ns_tree SET rgt = rgt + 2 WHERE rgt >= @treeRight;
       UPDATE ns_tree SET lft = lft + 2 WHERE lft > @treeRight;
       INSERT INTO ns_tree VALUES(0, '$name', @treeRight , @treeRight+1 , '$level','$show_me','$adress','$side');
      COMMIT;",true);
      }

      function add_r($id,$name,$show_me=true,$adress="",$side="left"){       $level=$this->get_info($id,"level");       $this->db->query("
      BEGIN;

SELECT @treeRight := rgt FROM ns_tree
WHERE id = '$id';

UPDATE ns_tree SET rgt = rgt + 2 WHERE rgt > @treeRight;
UPDATE ns_tree SET lft = lft + 2 WHERE lft > @treeRight;

INSERT INTO ns_tree VALUES(0, '$name', @treeRight + 1, @treeRight + 2, '$level','$show_me','$adress','$side');

COMMIT;

      ",true);
      }

    function del_cat($type,$id){    	if ($type=="doc") {    		$this->db->query("DELETE FROM ns_doc WHERE id='$id'");    		  }else{
     			$this->db->query("
     BEGIN;

SELECT @treeLeft := lft, @treeRight := rgt, @treeWidth := rgt - lft + 1
FROM ns_tree
WHERE id = '$id';

DELETE FROM ns_tree WHERE lft BETWEEN @treeLeft AND @treeRight;


UPDATE ns_tree SET rgt = rgt - @treeWidth WHERE rgt > @treeRight;
UPDATE ns_tree SET lft = lft - @treeWidth WHERE lft > @treeRight;

COMMIT;
    ",true);
    	}
    }

    function return_doc($id,$module){
	$ret=$this->db->result_array($this->db->query("SELECT form_doc.id,id_lang_text.name,id_lang_text.text  FROM id_lang_text, ns_doc,form_doc  WHERE ns_doc.father_id='$id' and ns_doc.id=form_doc.id  and form_doc.id_lang_text = id_lang_text.id"));
    return $ret;
	}

	function registry($class,$name,$schedule,$standby,$side_schedule,$side_standby,$templates,$name_module,$help_an){     $this->db->query("INSERT INTO modules VALUES(0,'$class','$name','$schedule','$standby','$side_schedule','$side_standby','$templates','$name_module','$help_an');");

	}

	function create_table($xml,$name){	 $sql="CREATE TABLE $name (";     foreach($xml->module_database->field as $f){      $sql_t.=$f." ".$f['type']." ".$f['modifier']." , " ;

     }
     $sql.=$sql_t.")";
     $sql=str_replace(" , )"," )",$sql);
     $this->db->query($sql);
	}

	function return_side($id=0){	 if($id==0){	  $tmp=$this->db->result_array($this->db->query("SELECT * From side_site WHERE id_template=( SELECT id From templates WHERE show_me='1')"));	  }else{      $tmp=$this->db->result_array($this->db->query("SELECT * From side_site WHERE id_template='$id'"));
      }
     return $tmp;
	}

	function return_config_mod(){	 $tmp=$this->db->result_array($this->db->query("SELECT * From modules"));
	 return $tmp  ;
	}

	function return_config_schedule(){
	 $tmp=$this->db->result_array($this->db->query("SELECT * From modules WHERE schedule='1'"));
	 return $tmp  ;
	}

	function return_config_standby(){
	 $tmp=$this->db->result_array($this->db->query("SELECT * From modules WHERE standby='1'"));
	 return $tmp  ;
	}

	function return_login_users(){     return $this->db->result_array($this->db->query("SELECT type From type_users;"));
	}

	function return_pass($login){	 $ret=$this->db->result_array($this->db->query("SELECT pass From type_users,users WHERE type_users.type='$login' and type_users.id=users.id"));
	 return $ret [0][pass];
	}

	function return_info_user($login){     return  $this->db->result_array($this->db->query("SELECT users.adress,users.name,type_users.type,type_users.level From type_users,users WHERE type_users.type='$login' and type_users.id=users.id"));

	}

	function load_cat_modules(){	 return $this->db->result_array($this->db->query("SELECT * From modules ORDER BY id;"));

	}

	function return_info_model($arg){	 return $this->db->result_array($this->db->query("SELECT * From modules WHERE id='$arg'ORDER BY id;"));
	}

	function return_info_template(){	 return $this->db->result_array($this->db->query("SELECT * From templates  WHERE show_me=1;"));
	}

	function return_all_templates(){	  return $this->db->result_array($this->db->query("SELECT * From templates;"));
	}

	function return_user(){     return $this->db->result_array($this->db->query("SELECT * From users as u, type_users as ut WHERE u.id=ut.id;"));
	}

	function return_element($arg){	 if($arg)
	   return $this->db->result_array($this->db->query("SELECT * From form_level WHERE id_level='$arg'"));
	  else
       return $this->db->result_array($this->db->query("SELECT * From form_level"));
	}

	function save_element($url,$level,$name,$id_level){	 $this->db->query("INSERT INTO form_level VALUES(0,'$url','$level','$name','$id_level')");
	}

	function return_level_form($url){	 return $this->db->result_array($this->db->query("SELECT level From form_level where url='$url'"));
	}

	function save_level($lv,$id){	 $this->db->query("UPDATE form_level SET level='$lv' WHERE id='$id';");
	}

	function return_user_type($id=""){	 if($id==""){	  return $this->db->result_array($this->db->query("SELECT * From type_users ;"));
	  }else{	 	return $this->db->result_array($this->db->query("SELECT * From type_users WHERE id='$id';"));
	  }
	}

	function save_user($name,$adress,$pass,$type,$name_p,$oper="insert"){	 if($oper=="insert")
	  $this->db->query("INSERT INTO users VALUES('$name','$adress','$pass',(SELECT id From type_users WHERE type='$type'));");
	   else
	    $this->db->query("UPDATE users SET name='$name', adress='$adress', pass='$pass', id=(SELECT id From type_users WHERE type='$type')  WHERE name='$name_p'");
	}

	function del_user($name){	 $this->db->query("DELETE FROM users WHERE name='$name';");
	}

	function save_login($login,$level,$id="insert"){     if($id=="insert"){	  $this->db->query("INSERT INTO type_users VALUES(0,'$login','$level');");
	  }else{	  	$this->db->query("UPDATE type_users SET type='$login',level='$level' WHERE id='$id' ;");
	  }
	}

	function del_login($id){	 $this->db->query("DELETE FROM type_users WHERE id='$id';");
	}

    function return_cat($arg=""){      		switch($arg){

         case"all":{
           $tmp=$this->db->result_array($this->db->query("SELECT node.id, node.name, node.level,node.show_me
FROM ns_tree AS node,
ns_tree AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.id = 1
ORDER BY node.lft;"));

         }break;

         case"cat":{
         $tmp=$this->db->result_array($this->db->query("SELECT node.id, node.name, node.level,node.show_me
FROM ns_tree AS node,
ns_tree AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.id ='$arg1'
ORDER BY node.lft;"));

         }break;
        default:break;
        }
        return $tmp;
    }

    function return_menu($p_id="Root",$side="",$lang){    	  $p_id=$this->get_info($p_id,"id","","ns_tree","name");          $sql_show=" and node1.show_me=1 ";
          $sql_module="and node1.module='$module' ";
     $sql_side=" and node1.side='$side' ";
     $sql_start="SELECT lang.text,node1.id, node1.name,node1.level,node1.adress,node1.templates,node1.module,node1.func_name,node1.alias
FROM ns_tree AS node1, ns_tree AS parent1,  id_lang_text AS lang ,(SELECT node.id, node.level FROM ns_tree AS node, ns_tree AS parent WHERE
     node.lft <= parent.lft
     AND
      node.rgt >= parent.rgt
      AND
      parent.id ='$p_id') AS b
WHERE node1.lft BETWEEN parent1.lft AND parent1.rgt AND parent1.id =b.id AND node1.level=(b.level+1) AND node1.alias=lang.id AND lang.lang='$lang' ";
	 $sql_end=" GROUP BY node1.id ORDER BY node1.lft;";
	 $sql=$sql_start;
     $sql=$sql.$sql_show;
     if ($side !="")$sql=$sql.$sql_side;
     $sql.=$sql_end;
     return $this->db->result_array($this->db->query($sql));

    }

    function save_side_l_un($name,$arg,$id){
     $arg=!$arg;     $this->db->query("UPDATE side_site SET free='$arg' WHERE side='$name' and id_template='$id'");
    }

    function save_side($name,$id){     $this->db->query("INSERT INTO side_site VALUE('$name',1,'$id');");
    }

    function save_tmp_inf ($id,$id_n,$path_file,$name,$show_me ){
     $this->db->query("UPDATE templates SET show_me='0';
     UPDATE templates SET id='$id_n', name='$name',path_file='$path_file', show_me='$show_me'   WHERE id='$id';",true);
    }

    function tem_del_side($side,$id){     $this->db->query("DELETE FROM side_site WHERE side='$side' AND id_template='$id' ");
    }

    function registry_template($name,$name_project,$path_file,$path_small_img,$show_me){     $this->db->query("INSERT INTO templates VALUE(0,'$path_file','$name','$show_me','$path_small_img','$name_project')");
    }

    function del_templates($p_n){     $this->db->query("DELETE From templates WHERE name_project='$p_n'");
    }

  }
?>